/*************************************************************
 * Title: 
 * Description: 
 * Author: Snow Huang
 * Email: Shaobin.Software@gmail.com Shaobin_Software@163.com
 * CreateTime: 2010-7-6 下午08:37:25
 * Copyright © 落羽社区 All right reserved
 ************************************************************/
package snow.biz.system.model;

import java.util.Date;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.util.Assert;

import snow.biz.system.dao.UserDao;
import snow.biz.system.model.query.UserQuery;
import snow.core.util.algorithm.encoding.PasswordEncoder;

/**
 * 
 */
public class UserFactory {

	private UserDao userDao;
	private PasswordEncoder passwordEncoder;

	public User createUser(String username, String password) {
		validate(username, password);
		return new User().setUsername(username).setPassword(passwordEncoder.encodePassword(password)).setRegisterTime(new Date());
	}

	/**
	 * @return the passwordEncoder
	 */
	public PasswordEncoder getPasswordEncoder() {
		return passwordEncoder;
	}

	/**
	 * @return the userDao
	 */
	public UserDao getUserDao() {
		return userDao;
	}

	/**
	 * @param passwordEncoder
	 *            the passwordEncoder to set
	 */
	public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
		this.passwordEncoder = passwordEncoder;
	}

	/**
	 * @param userDao
	 *            the userDao to set
	 */
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	/**
	 * 验证用户名、密码是否输入正确
	 * 
	 * @param username
	 * @param password
	 */
	private void validate(String username, String password) {
		Assert.hasText(username, "");
		Assert.hasText(password, "");
		Assert.isTrue(CollectionUtils.isEmpty(getUserDao().findUsers(new UserQuery().andUsernameEquals(username))), "");
	}
}
